﻿using BLL;
using Function;
using Model;
using Model.Permission;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace UI
{
    public partial class GetPwd : System.Web.UI.Page
    {
         ST_InvestorBLL investor = new ST_InvestorBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["yz"].ToString().Trim()=="yes")
                {
                    if (!investor.ExistsByMainTel(Request["phone"]))
                    {
                        Response.Write(-100);
                        return;
                    }
                    else
                    {
                        try
                        {
                            ST_Investor credit = investor.GetModel(Request["phone"].ToString().Trim());

                            if (credit == null || credit.State != 1)
                            {
                                Response.Write(-200);
                                return;
                            }
                        }
                        catch (Exception)
                        {
                            Response.Write(-200);
                            return;
                        }
                    }
                }
                Random rd = new Random();
                string str = "0123456789";
                string result = "";
                for (int i = 0; i < 6; i++)
                {
                    result += str[rd.Next(str.Length)];
                }
                Session["dtpwd"] = result;
                Session["type"] = Request["type"];
                Session["phone"] = Request["phone"];
                if (Request["type"] == "getcode")
                {
                    GetPwds(Request["phone"], "你的动态密码为:" + result + ",请妥善保存![酒聚汇]");
                }
                else
                {
                    GetPwds(Request["phone"], "你的验证码为:" + result + ",请妥善保存![酒聚汇]");
                }
            }
        }
        public void GetPwds(string phone, string data)
        {
            string replay = Method.z_SendSMS(phone, data);
            string[] aa = GetTaoBaoIdList(replay);
            if (aa[0].Trim() == "-1")
            {
                Response.Write("-1");
            }
            else
            {
                Response.Write("0");
            }
        }
        public static string[] GetTaoBaoIdList(string sHtmlText)
        {
            // 定义正则表达式用来匹配 img 标签//^mm_[0-9]{8}_[0-9]+_[0-9]$
            Regex regPid = new Regex(@"-[0-9]+", RegexOptions.IgnoreCase);

            // 搜索匹配的字符串
            MatchCollection matches = regPid.Matches(sHtmlText);
            int i = 0;
            string[] sUrlList = new string[matches.Count];

            // 取得匹配项列表
            foreach (Match match in matches)
                sUrlList[i++] = match.ToString();
            return sUrlList;
        }
    }

}